package com.cn.goodsadmin.common.frame;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class NetService {

  private static volatile NetService service;
  private static byte lock[] = new byte[0];
  public static ThreadPoolExecutor netWorkThreadPoolExecutor;

  private NetService() {
    netWorkThreadPoolExecutor =new ThreadPoolExecutor
      (100,
        1000,
        0L,
        TimeUnit.MILLISECONDS,
        new ArrayBlockingQueue<Runnable>(200));

  }

  public static NetService getInstance() {
    if (service == null) {
      synchronized (lock) {
        if (service == null) {
          service = new NetService();
        }
      }
    }
    return service;
  }


  public BaseNetAns startSynHttpNetWork(final RequestInfo info) {
    BaseNetWork netWork = NetFactory.getInstance()
      .creatHttpNetWork(info);
    BaseNetAns ans = NetFactory.getInstance().creatHttpAns(info);
    netWork.setNetWorkAgecny(ans);
    netWork.startNetWork();
    return ans;
  }

  public void startAsyHttpNetWork(final RequestInfo info){
    Runnable net = new Runnable() {

      @Override
      public void run() {
        // TODO Auto-generated method stub
        BaseNetWork netWork = NetFactory.getInstance()
          .creatHttpNetWork(info);
        BaseNetAns ans = NetFactory.getInstance().creatHttpAns(info);
        netWork.setNetWorkAgecny(ans);
        netWork.startNetWork();
      }
    };
    netWorkThreadPoolExecutor.submit(net);
  }
}
